home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-22 | 6.9 KB | 240 lines | [TEXT/CWIE] |
- ///--------------------------------------------------------------------------------------
- // Stats.c - routines for displaying the stats portion of the screen
- //
- // Created 9/20/97
- ///--------------------------------------------------------------------------------------
-
- #include <SWIncludes.h>
- #include <SWGameUtils.h>
- #include <SWApplication.h>
-
- #include "Application.h"
- #include "Stats.h"
- #include "Shark Attack.h"
- #include "GlobalVariables.h"
-
-
- StatsRec gLivesRec, gLevelRec, gScoreRec;
-
-
- ///--------------------------------------------------------------------------------------
- // InitStats - create offscreen and window frames for stats area. This function is
- // called only once when the program first start up.
- ///--------------------------------------------------------------------------------------
-
- void InitStats( void )
- {
- PixPatHandle statsPixPatH;
- Rect statsRect;
- OSErr err;
-
- statsRect = gSpriteWorldP->windRect;
- statsRect.bottom = statsRect.top + kStatsHeight;
-
- SetPort(gWindowP);
- err = SWCreateWindowFrame(&gStatsWindowFrameP, &statsRect, 0);
- FatalError(err);
-
- OffsetRect(&statsRect, -statsRect.left, -statsRect.top);
- err = SWCreateFrame(gSpriteWorldP->mainSWGDH, &gStatsBackFrameP, &statsRect,
- gSpriteWorldP->pixelDepth);
- FatalError(err);
-
- SWLockWindowFrame(gStatsWindowFrameP);
- SWLockFrame(gStatsBackFrameP);
-
- statsPixPatH = GetPixPat(131);
- if (statsPixPatH == NULL)
- CantFindResource();
-
- // Fill stats area with grey
- statsRect = gStatsBackFrameP->frameRect;
- SetGWorld(gStatsBackFrameP->framePort, nil);
- FillCRect(&statsRect, statsPixPatH);
- DisposePixPat(statsPixPatH);
-
- // Draw a black & white frame around the stats area
- ForeColor(whiteColor);
- FrameRect(&statsRect);
-
- InsetRect(&statsRect, 1, 1);
- ForeColor(blackColor);
- FrameRect(&statsRect);
-
- // Set certain fields of each StatsRec
- gLivesRec.theFrameP = gStatsWindowFrameP;
- gLivesRec.numDigits = 1;
- gLivesRec.justification = kRightJustify;
-
- gLevelRec.theFrameP = gStatsWindowFrameP;
- gLevelRec.numDigits = 2;
- gLevelRec.justification = kRightJustify;
-
- gScoreRec.theFrameP = gStatsWindowFrameP;
- gScoreRec.numDigits = 5;
- gScoreRec.justification = kLeftJustify;
-
- // Load the picture for each area (lives, level, and score)
- DrawPictInStatsArea(kLivesPictResID);
- DrawPictInStatsArea(kLevelPictResID);
- DrawPictInStatsArea(kScorePictResID);
-
- // Load the picture containing our digits into a Frame
- err = SWCreateFrameFromPictResource(gSpriteWorldP, &gStatsNumberFrameP,
- kNumbersPictResID, 0, kNoMask);
- FatalError(err);
-
- SWLockFrame(gStatsNumberFrameP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // DrawPictInStatsArea - used by InitStats. Assumes correct port is already set.
- ///--------------------------------------------------------------------------------------
-
- void DrawPictInStatsArea( short pictID )
- {
- PicHandle myPictH;
- Rect pictRect;
-
- myPictH = GetPicture(pictID);
- if (myPictH != NULL)
- {
- pictRect = (**myPictH).picFrame;
- CenterRect(&pictRect, &gStatsBackFrameP->frameRect);
-
- // Determine where to draw pict, and save position for drawing numbers later
- if (pictID == kLivesPictResID)
- {
- OffsetRect(&pictRect, -pictRect.left + 40, 0);
- gLivesRec.horizLoc = pictRect.left + kLivesNumOffset;
- gLivesRec.vertLoc = pictRect.top + kStatsVertOffset;
- }
- else if (pictID == kLevelPictResID)
- {
- OffsetRect(&pictRect, -20, 0);
- gLevelRec.horizLoc = pictRect.left + kLevelNumOffset;
- gLevelRec.vertLoc = pictRect.top + kStatsVertOffset;
- }
- else // kScorePictResID
- {
- OffsetRect(&pictRect, gStatsBackFrameP->frameRect.right -
- pictRect.right - 40, 0);
- gScoreRec.horizLoc = pictRect.left + kScoreNumOffset;
- gScoreRec.vertLoc = pictRect.top + kStatsVertOffset;
- }
-
- DrawPicture(myPictH, &pictRect);
- ReleaseResource((Handle)myPictH);
- }
- else
- CantFindResource();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // UpdateStatsArea - copies the contents of the gStatsBackFrameP to the gStatsWindowFrameP
- ///--------------------------------------------------------------------------------------
-
- void UpdateStatsArea( void )
- {
- SetGWorld(gStatsWindowFrameP->framePort, nil);
-
- (*gSpriteWorldP->screenDrawProc)(gStatsBackFrameP, gStatsWindowFrameP,
- &gStatsBackFrameP->frameRect, &gStatsWindowFrameP->frameRect);
-
- ResetStats(); // Force the numbers to redraw, by making them think they've changed
- UpdateStatsNumbers(); // Then draw them
- }
-
-
- ///--------------------------------------------------------------------------------------
- // UpdateStatsNumbers - redraws the numbers for each stats area if they've changed
- ///--------------------------------------------------------------------------------------
-
- void UpdateStatsNumbers( void )
- {
- // Stuff lives, level, and score data into each StatsRec
- gLivesRec.theNum = gNumLivesLeft;
- gLevelRec.theNum = gCurrentLevel;
- gScoreRec.theNum = gScore;
-
- UpdateStatsRec(&gLivesRec);
- UpdateStatsRec(&gLevelRec);
- UpdateStatsRec(&gScoreRec);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ResetStats - called before a new game to make sure the new stats get drawn
- ///--------------------------------------------------------------------------------------
-
- void ResetStats( void )
- {
- gLivesRec.oldNum = -1;
- gLevelRec.oldNum = -1;
- gScoreRec.oldNum = -1;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // UpdateStatsRec - redraws the number for a particular statsRec if it has changed
- ///--------------------------------------------------------------------------------------
-
- void UpdateStatsRec( StatsRecPtr statsRecP )
- {
- short theRow, theCol, theNum, theDigit;
- short length = 0;
- Rect srcRect, dstRect;
-
- // Only draw the number if it has changed since last time
- if (statsRecP->oldNum != statsRecP->theNum)
- {
- statsRecP->oldNum = statsRecP->theNum;
-
- theRow = statsRecP->theFrameP->frameRect.top + statsRecP->vertLoc;
- theCol = statsRecP->theFrameP->frameRect.left + statsRecP->horizLoc;
- theNum = statsRecP->theNum;
- if (theNum < 0)
- theNum = 0;
-
- do // calculate length of the number
- {
- length++;
- theNum /= 10;
- } while (theNum > 0);
-
- if (statsRecP->justification == kLeftJustify)
- theCol += (length-1) * kNumberWidth;
- else
- theCol += (statsRecP->numDigits-1) * kNumberWidth;
-
- theNum = statsRecP->theNum;
- if (theNum < 0)
- theNum = 0;
-
- do
- {
- theDigit = theNum%10;
-
- srcRect.top = 0;
- srcRect.bottom = kNumberHeight;
- srcRect.left = theDigit * kNumberWidth;
- srcRect.right = srcRect.left + kNumberWidth;
-
- dstRect.left = theCol;
- dstRect.top = theRow;
- dstRect.right = theCol + kNumberWidth;
- dstRect.bottom = theRow + kNumberHeight;
-
- // Copy the digit using the SpriteWorld's current screenDrawProc
- (*gSpriteWorldP->screenDrawProc)(gStatsNumberFrameP, gStatsWindowFrameP,
- &srcRect, &dstRect);
-
- theCol -= kNumberWidth;
- theNum /= 10;
- } while (theNum > 0);
- }
- }
-
-